package scuba.DiveBuddy.Class;

import android.util.Log;
import org.joda.time.DateTime;
import scuba.DiveBuddy.data.DiverProfileSource;

/**
 * @Author Alex Anderson
 * @Version 2
 * @Since 19/01/14.
 *
 * Containing static methods for accessing and manipulating the UserProfileTable
 */
public class DiverProfile {

    private static DiverProfile _Active; //singleton
    public static DiverProfile getActiveProfile()
    {
        Log.w("DiverProfile", "Request made");
        if(_Active == null) {
            Log.w("DiverProfile", "_Active == null retrieving");
            _Active = DiverProfileSource.GetActiveProfile();
        }

        Log.w("DiverProfile", "returning = " + _Active);
        return _Active;
    }

    private int DiverID;
    private String DiverForename;
    private String DiverSurname;
    private DateTime DiverDOB;
    private String ICEContact;
    private String ICENumber;
    private String AllergyNotes;
    private String MedicineNotes;

    //This ctor creates a new User
    private DiverProfile(String forename, String surname, DateTime dob)
    {
        this(DiverProfileSource.CreateUser(forename, surname, dob), forename, surname, dob); //Will Create a new user
    }

    //This ctor is for creating instance via database
    public DiverProfile(int id, String forename, String surname, DateTime dob)
    {
        this.DiverID = id;
        this.DiverForename = forename;
        this.DiverSurname = surname;
        this.DiverDOB = dob;
        this.ICEContact = "";
        this.ICENumber = "";
        this.AllergyNotes = "";
        this.MedicineNotes = "";
    }
    //This ctor is for creating instance via database
    public DiverProfile(int id, String forename, String surname, DateTime dob, String icecontact, String icenumber, String allergy, String med)
    {
        this.DiverID = id;
        this.DiverForename = forename;
        this.DiverSurname = surname;
        this.DiverDOB = dob;
        this.ICEContact = icecontact == null ? "" : icecontact;
        this.ICENumber = icenumber == null ? "" : icenumber;
        this.AllergyNotes = allergy == null ? "" : allergy;
        this.MedicineNotes = med == null ? "" : med;
    }

    //Accessors & Mutators
    public int getID() { return this.DiverID; }
    //public bool setID() { return this.UserID; } NEVER DO THIS TO A PK

    public String getForename() { return this.DiverForename; }
    public Boolean setForename(String newForename) {
        if(getForename().equals(newForename))
            return true;

        if(!DiverProfileSource.ModifyForename(this, newForename))
            return false;

        this.DiverForename = newForename;
        return true;
    }

    public String getSurname() { return this.DiverSurname; }
    public Boolean setSurname(String newSurname) {
        if(getSurname().equals(newSurname))
            return true;

        if(!DiverProfileSource.ModifySurname(this, newSurname))
            return false;

        this.DiverSurname = newSurname;
        return true;
    }

    public DateTime getDOB() { return this.DiverDOB; }
    public Boolean setDOB(DateTime dob) {
        if(getDOB().equals(dob))
            return true;

        if(!DiverProfileSource.ModifyDOB(this, dob))
            return false;

        this.DiverDOB = dob;
        return true;
    }

    public int getAge() {
        return -1; //todo: implement age from doB;
    }
    public String getName() { return getForename() + " " +  getSurname(); }

    public String getICEContact() { return this.ICEContact; }
    public Boolean setICEContact(String contact) {
        if(getICEContact().equals(contact))
            return true;

        if(!DiverProfileSource.ModifyICEContact(this, contact))
            return false;

        this.ICEContact = contact;
        return true;
    }

    public String getICENumber() { return this.ICENumber; }
    public Boolean setICENumber(String number) {
        if(getICENumber().equals(number))
            return true;

        if(!DiverProfileSource.ModifyICENumber(this, number))
            return false;

        this.ICENumber = number;
        return true;
    }

    public String getAllergyNotes() { return this.AllergyNotes; }
    public Boolean setAllergyNotes(String notes) {
        if(getAllergyNotes().equals(notes))
            return true;

        if(!DiverProfileSource.ModifyAllergy(this, notes))
            return false;

        this.AllergyNotes = notes;
        return true;
    }

    public String getMedicineNotes() { return this.MedicineNotes; }
    public Boolean setMedicineNotes(String notes) {
        if(getMedicineNotes().equals(notes))
            return true;

        if(!DiverProfileSource.ModifyMedicine(this, notes))
            return false;

        this.MedicineNotes = notes;
        return true;
    }

    @Override
    public String toString() {
        return "DiverProfile: N: " + getForename() + " " + getSurname();
    }

    public static DiverProfile createAndRegisterUser(String forename, String surname, DateTime dob)
    {
        _Active = new DiverProfile(forename, surname, dob);
        return _Active;
    }
}
